from owlready2 import *

onto_path.append('./owl')
# my_onto = get_ontology('onto.owl') # 这样写也可以，更简单
my_onto = get_ontology("http://test.org/onto/")
pizza_onto = get_ontology('http://www.co-ode.org/ontologies/pizza/pizza.owl').load()
def save():
    with my_onto:
        class Pizza(Thing):
            pass
        class id(Pizza >> str, DataProperty):
            pass
        a = Pizza('a')
        my_onto.save()

def load():
    my_onto = get_ontology('onto.owl').load()
    print(my_onto.Pizza)

def print_all(items):
    for item in items:
        print('\t', item)

def attribute():
    print(pizza_onto.base_iri)
    print(pizza_onto.imported_ontologies)

def properties():
    
    # 调用onto所有的方法
    print('1. classes')
    print_all(pizza_onto.classes())
    print('2. individuals')
    print_all(pizza_onto.individuals())
    print('3. object_properties')
    print_all(pizza_onto.object_properties())
    print('4. annotation_properties')
    print_all(pizza_onto.annotation_properties())
    print('5. properties')
    print_all(pizza_onto.properties())
    print('6. disjoint_classes')
    print_all(pizza_onto.disjoint_classes())
    print('7. disjoint_properties')
    print_all(pizza_onto.disjoint_properties())
    print('8. disjoints')
    print_all(pizza_onto.disjoints())
    print('9. different_individuals')
    print_all(pizza_onto.different_individuals())
    print('10. get_namspace')
    print(pizza_onto.get_namespace(pizza_onto.base_iri))

def iris():
    print(IRIS["http://www.lesfleursdunormal.fr/static/_downloads/pizza_onto.owl#Pizza"])

def simple_queries():
    # print(pizza_onto.search(iri='*Topping'))
    # print(pizza_onto.search(type=pizza_onto.Pizza))
    # print(pizza_onto.search(subclass_of=pizza_onto.Pizza))
    # print(pizza_onto.search(is_a=pizza_onto.Pizza))
    # print(pizza_onto.search(subpropertyof=pizza_onto.hasTopping))
    # print(pizza_onto.search(has_topping="*"))
    # print(pizza_onto.search(Pizza="*"))#不行
    # print(pizza_onto.search(Pizza="AmericanHot"))#不行
    # print(pizza_onto.search_one(is_a = pizza_onto.Pizza))
    print(pizza_onto.search(is_a = pizza_onto.Pizza, has_topping = pizza_onto.search(is_a = pizza_onto.TomatoTopping)))
    # 特定参数: _use_str_as_loc_str, _case_sensitive, _bm25

def metadata():
    my_onto.metadata.comment.append('my first comment')
    for annotation_property in my_onto.metadata:
        print(annotation_property, ': ', annotation_property[my_onto.metadata])

def importing_other_ontologies():
    my_onto.load()
    my_onto.imported_ontologies.append(pizza_onto)
    print_all(my_onto.classes()) # 没有算到my_onto里边去, 没有报错应该是引入了

def save_as():
    my_onto.load()
    my_onto.save(file = "owl/test1.rdf", format = "rdfxml")
    my_onto.save(file = "owl/test2.ntriples", format = "ntriples")

def change_base_iri():
    print(my_onto.base_iri)
    my_onto.set_base_iri("http://test.org/new_base_iri.owl#", rename_entities = False)
    print(my_onto.base_iri)

def destory():
    print(my_onto.base_iri)
    my_onto.distroy()
    print(my_onto.base_iri)

def destory_update():
    #不是很理解
    my_onto.destroy(update_relation = True, update_is_a = True)

# save()
# load()
# attribute()
# properties()
# simple_queries()
# metadata()
# importing_other_ontologies()
# save_as()
# change_base_iri()
# destory()
destory_update()